package com.itboye.jinpuyiyuan.activity.login;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/19 0019.
 * 找回密码
 */

public class FindPasswordActivity extends BaseActivity implements Observer {
    //title

    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar,include_layout;

    private EditText etVertifyCode,etPhone,etResetPassword,etVertifyPassword;
    private TextView sendVerifyCode,tvSure;
    private LinearLayout phoneLl;
    private BranchAddressPresenter presenter,presenter1;
    private String phoneNumber;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_findpassword);
        include_layout.setBackgroundColor(Color.TRANSPARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        initView();

    }


    private void initView() {
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("找回密码");
        presenter= new BranchAddressPresenter(this);
        presenter1= new BranchAddressPresenter(this);
        phoneNumber=getIntent().getStringExtra("phoneNumber");
        etPhone.setText(phoneNumber);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.rlLeft:
            case R.id.leftImg:

                this.finish();
                break;
            case R.id.phoneLl:
//                MAlert.alert("");
                break;
                //发送验证码
            case R.id.sendVerifyCode:
                    if (etPhone.getText().length()>20||etPhone.getText().length()<6){
                        MAlert.alert("请输入电5-20位号码");
                        return;
                    }
                //    login_corner_blueedge

                //          发送类型(友盟推送[可用于测试推送]：push_umeng, 短信腾讯云：sms_qcloud, 短信聚合: sms_juhe, 返回验证码：alert)[必须]
                //. 验证码类型(1: 注册 2: 更新密码 3: 新绑定手机号 4: 更换手机号 5: 登录 6: 找回密码 7:实名认证)[必须]
                sendVerifyCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.illessinfo_corner_blue));
                sendVerifyCode.setTextColor(getResources().getColor(R.color.white));
                presenter.sendVerifyCode(etPhone.getText().toString(),"2","sms_juhe");
                sendVerifyCode.setEnabled(false);
                break;

                //确定
            case R.id.tvSure:

                if (etPhone.getText().toString()==null||etPhone.getText().toString().equals("")
                        ||etVertifyCode.getText().toString().equals("")||etResetPassword.getText().toString().equals("")
                        ||etResetPassword.getText().toString()==null||etVertifyPassword.getText().toString().equals("")
                        ||etVertifyPassword.getText().toString()==null){
                    MAlert.alert("请填写完整信息");
                }else if (etPhone.getText().length()>20||etPhone.getText().length()<5){
                    MAlert.alert("请输入5-20位的手机号码");
                }else if (!etResetPassword.getText().toString().equals(etVertifyPassword.getText().toString())){
                    MAlert.alert("两次密码不一致");
                }else {

                    //. 验证码类型(1: 注册 2: 更新密码 3: 新绑定手机号 4: 更换手机号 5: 登录 6: 找回密码 7:实名认证)[必须]
                    presenter.verifyCode(etPhone.getText().toString(),etVertifyCode.getText().toString(),"2");
                }
                break;


        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){


            if (resultEntity.getEventType()==BranchAddressPresenter.By_UserAccount_updatePwdByMobile_success){

                SPUtils.put(FindPasswordActivity.this,null, Const.LOGIN_PASSWORD,etResetPassword.getText().toString());
//                MAlert.alert("密码修改成功");
                FindPasswordActivity.this.finish();
            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_UserAccount_updatePwdByMobile_fail){

//                 MAlert.alert("密码修改失败"+resultEntity.getMsg());
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_SecurityCode_send_success){
                MAlert.alert("验证码发送成功请注意接受");
                sendVerifyCode.setEnabled(true);
                sendVerifyCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
                sendVerifyCode.setTextColor(getResources().getColor(R.color.title_blue));
            } else if (resultEntity.getEventType()==BranchAddressPresenter.By_SecurityCode_send_fail){
                MAlert.alert(resultEntity.getMsg());
                sendVerifyCode.setEnabled(true);
                sendVerifyCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
                sendVerifyCode.setTextColor(getResources().getColor(R.color.title_blue));
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_SecurityCode_verify_success){
                phoneNumber=etPhone.getText().toString();
                presenter.forgetPassword(phoneNumber,etVertifyCode.getText().toString(),etResetPassword.getText().toString());

            }else if (resultEntity.getEventType() == BranchAddressPresenter.By_SecurityCode_verify_fail){

                MAlert.alert(resultEntity.getMsg());
            }


//
//            else if (resultEntity.getEventType()== BranchAddressPresenter.By_SecurityCode_create_success){
//                String data = (String) resultEntity.getData();
//                etVertifyCode.setText(data);
////                MAlert.alert("验证码获取成功");
//            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_SecurityCode_create_fail){
////                MAlert.alert("验证码获取失败");
//            }
        }
    }
}
